#ifndef __PARK_DEV_UNIT_H__
#define __PARK_DEV_UNIT_H__

#define NAME_LEN 120
#define PARK_HASH_SIZE 100
#define MAX_OFFLINE_DEV_NUM 500
#define MAX_PROBER_STAT_TIME 40
#define MIN_PROBER_STAT_TIME 10

#define SENSOR_STAT_OFFLINE 0x2
#define SENSOR_STAT_NOCAR 0x5
#define SENSOR_STAT_HAVECAR 0x4
#define SENSOR_STAT_ERROR 0x3
#define SENSOR_STAT_IDLE 0x7
#define SENSOR_STAT_BUSY 0x8

#define PARK_DEV_TYPE_PROBER 0x1
#define PARK_DEV_TYPE_SENSOR 0x2

typedef struct led_s S_LED, *PS_LED;

typedef struct sensor_s
{
	int dev_type;
	int stat;
	int old_stat;
	int high;
	int sensor_id;
	int sensor_type;
	int io_addr;
	int host_addr;
	struct list_head sensor_list;
}S_SENSOR, *PS_SENSOR;

#define PROBER_IO_NUM 8
#define PROBER_STAT_ONLINE 0x1
#define PROBER_STAT_OFFLINE 0x0
#define PROBER_STAT_UPDATA 0x2
#define PROBER_STAT_REQ 0x3


typedef struct prober_s
{
	int dev_type;
	int stat;
	int prober_id;
	int prober_type;
	int area_id;
	time_t updata_time;
	//int old_stat;
	//PS_SENSOR pstSensor_a[PROBER_IO_NUM];
	int sensor_cnt;
	struct list_head sensor_list;
	char ip_addr[20];
	struct list_head prober_list;
}S_PROBER, *PS_PROBER;

typedef struct area_s
{
	int area_id;
	char area_name[NAME_LEN];
	//int prober_num;
	//PS_PROBER pstProber;
	struct list_head prober_list;
	int area_car_num;
	int area_car_num_old;
	struct list_head area_list;
	struct list_head led_list;
	struct list_head area_direct_list;
	//PS_LED pstLed;
	//struct list_head area_direct_list;
	struct list_head area_sys_list;
}S_AREA, *PS_AREA;

typedef struct floor_s
{
	char floor_name[NAME_LEN];
	int floor_id;
	int floor_no;
	int area_cnt;
	struct list_head area_list;
	//PS_AREA pstArea;
	struct list_head floor_list;
}S_FLOOR, *PS_FLOOR;

#define DIRECT_WESTSOUTH    0
#define DIRECT_WEST			1
#define DIRECT_WESTNORTH	2
#define DIRECT_NORTH		3
#define DIRECT_EASTNORTH	4
#define DIRECT_EAST			5
#define DIRECT_EASTSOUTH	6


typedef struct direction_s
{
	int direction_type;
	char direction_name[NAME_LEN];
	//int area_num;
	struct list_head area_list;
	//PS_AREA *ppstArea;
}S_DIRECT, *PS_DIRECT;

#define LED_STAT_ONLINE 1
#define LED_STAT_OFFLINE 2
#define LED_DIRECT_MAX 7

struct led_s
{
	int stat;
	int led_type;
	int led_id;
	PS_DIRECT pstDirect_a[LED_DIRECT_MAX];
	char ip_addr[20];
	struct list_head led_list;
	struct list_head led_area_list;
};

#define PARK_STAT_INIT 0
#define PARK_STAT_START 1
#define PARK_STAT_REQ 2
#define PARK_STAT_RUN_INIT 3
#define PARK_STAT_RUN  4
#define PARK_STAT_PUSH 5
#define PARK_STAT_UPDATA 6
#define PARK_STAT_IDLE 7

extern char *stat_str[8];

#define PARK_STAT_IDLE_TIMEOUT 8
typedef struct park_info_s
{
	int park_stat;
	int park_stat_old;
	int cur_config_id;
	pthread_t pthread_id;
	unsigned short seq_id;
	unsigned short seq_req;
	unsigned short seq_ack;
	NET_HANDLE_T stNet;
	NET_SOCKET_T listen_fd;
	NET_SOCKET_T connect_fd;
	NET_SOCKET_T prober_connect_fd;
	NET_SOCKET_T prober_listen_fd;
	NET_SOCKET_T led_fd;
	sem_t park_sem;
	

	mxml_node_t *pstMxmlTopNode;
	mxml_node_t *pstMxmlTmpNode;
//	int commu_seq;
	pthread_mutex_t park_lock;
	int led_cnt;
	struct list_head led_list;
	//PS_LED pstLed;
	int floor_cnt;
	int prober_cnt;
	int sensor_cnt;
	//int led_cnt;
	//PS_FLOOR pstFloor;
	struct list_head floor_list;
	int area_cnt;
	struct list_head area_list;
	int prober_num_online;
	int prober_num_offline;

	int dev_offline_num;
	int *dev_offline_list;
	struct hash_list *pstHash;
	char server_addr[20];
	int server_port;
}S_PARK_INFO, *PS_PARK_INFO;

S_PARK_INFO stPark;

int park_get_stat();
void park_set_stat(int stat);
void park_stat_backup();
void park_stat_reco();
void park_seq_req();
int park_seq_is_same(int seq);
int park_seq_get();
void park_seq_push_set(int seq);
int park_seq_push_get();
void* park_stat_control(void *arg);












#endif
